package com.chenkun.service;

import com.chenkun.dao.ArticleDao;
import com.chenkun.model.Article;
import com.chenkun.model.RssEntry;
import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Description;
import com.sun.syndication.feed.rss.Guid;
import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.WireFeedOutput;
import com.sun.syndication.io.XmlReader;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chenkun on 2017/5/8.
 */
@Service
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private SolrClient solrClient;

    public List<Article> getAll(){
        List<Article> articles = this.articleDao.getAll();
        return articles;
    }

    public Article getByCode(String code){
        Article article = this.articleDao.getByCode(code);
        return article;
    }

    public int add(Article article){
        return this.articleDao.add(article);
    }

    public int delete(String code){
        return this.articleDao.delete(code);
    }

    public int update(Article article){
        return this.articleDao.update(article);
    }

    public List<Article> getByKeyWords(String keywords)  {
        List<Article> articles = new ArrayList<>();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","title:"+keywords+" OR content:"+keywords+" OR assess:"+keywords+" OR type: "+keywords+" OR target:"+keywords);
        solrQuery.setStart(0);
        solrQuery.setRows(10);
        QueryResponse response = null;
        try {
            response = solrClient.query(solrQuery);
        } catch (SolrServerException e) {
            return articles;
        } catch (IOException e) {
            return articles;
        }
        SolrDocumentList results = response.getResults();
        for (SolrDocument meta : results ){
            Article metaArticle = new Article();
            metaArticle.setCode(meta.get("code").toString());
            metaArticle.setTitle(meta.get("title").toString());
            metaArticle.setContent(meta.get("content").toString());
            metaArticle.setAssess(meta.get("assess").toString());
            metaArticle.setTarget(meta.get("target").toString());
            metaArticle.setType(meta.get("type").toString());
            articles.add(metaArticle);
        }
        return articles;
    }

    public List<RssEntry> getRss(String urlAddress){
        List<RssEntry> rssLists = new ArrayList<>();
        try {
            URL url = new URL(urlAddress);
            XmlReader reader = new XmlReader(url);
            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(reader);
            List entries = feed.getEntries();
            for (int i = 0; i < entries.size(); i++) {
                RssEntry rssEntry = new RssEntry();
                SyndEntry entry = (SyndEntry) entries.get(i);
                SyndContent description = entry.getDescription();
                rssEntry.setTitle(entry.getTitle());
                rssEntry.setDescr(description.getValue());
                rssEntry.setUrl(entry.getLink());
                rssLists.add(rssEntry);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rssLists;
    }

    public String createXml(){
        List<Article> articles = this.getAll();
        Channel channel = new Channel("rss_2.0");
        channel.setTitle("channel标题");
        channel.setDescription("channel的描述");
        channel.setLink("localhost");
        channel.setEncoding("utf-8");
        channel.setLanguage("zh-cn");
        channel.setTtl(5);
        channel.setPubDate(new Date());
        List<Item> items = new ArrayList<Item>();
        for (Article article : articles){
            Item item = new Item();
            item.setTitle(article.getTitle());
            item.setGuid(new Guid());
            item.setPubDate(new Date());
            Description description = new Description();
            description.setValue(article.getContent());
            item.setDescription(description);
            items.add(item);
        }
        channel.setItems(items);
        WireFeedOutput out = new WireFeedOutput();
        try {
            String xml=out.outputString(channel);
            System.out.println(xml);
            return xml;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return null;
        } catch (FeedException e) {
            e.printStackTrace();
            return null;
        }
    }




}
